
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.math.*;

import jxl.Cell;
import jxl.CellType;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class ReadToText {

	private String inputFile;
	List<Double> listaNumeri = new ArrayList<Double>();
	private String[] nome_foglio;			// vettore di stringhe contenente i nomi dei fogli excel
  
	public void setInputFile(String inputFile) {
		this.inputFile = inputFile;
	}

	public void read() throws IOException  {
		File inputWorkbook = new File(inputFile);
		Workbook w;
    
		try {
			w = Workbook.getWorkbook(inputWorkbook);
			nome_foglio = w.getSheetNames();		// salva i vari nomi dei fogli di lavoro
			// Get the first sheet
			for (int n=0; n<w.getNumberOfSheets(); n++){
				Sheet sheet = w.getSheet(n);		// get the n-sheet
				// Loop over first 10 column and lines
				for (int j = 0; j < sheet.getColumns(); j++) {
					for (int i = 0; i < sheet.getRows(); i++) {
						Cell cell = sheet.getCell(j, i);
						CellType type = cell.getType();
						if (type == CellType.LABEL) {
							System.out.println("I got a label " + cell.getContents());
						}

						if (type == CellType.NUMBER) {
							// con replaceAll sostituisco le , dei numeri xls con i . per lavorare in java
							//System.out.println("I got a number " + cell.getContents().replaceAll(",", "."));
							listaNumeri.add(Double.parseDouble(cell.getContents().replaceAll(",", ".")));
						}
					}
				}
				stampaLista(n);
				listaNumeri.clear();
			}
		} catch (BiffException e) {
			e.printStackTrace();
		}
	}

	public void stampaLista(int n){
		double sommaArea=0;
		System.out.println("Foglio lavoro[" + n + "] = " + nome_foglio[n]);
		for (int k=0; k< listaNumeri.size(); k++){
			System.out.println(listaNumeri.get(k));
			
			sommaArea = sommaArea + calcolaSuperficieCanna(listaNumeri.get(k)); 
		}
		System.out.println("Superficie totale canne:" + sommaArea);
	}
	
	public double calcolaSuperficieCanna(double canna){	// calcola la superficie di una canna
		return (Math.pow(canna/2.0, 2))*Math.PI;
	}
  
	public static void main(String[] args) throws IOException {
		ReadToText test = new ReadToText();
		test.setInputFile("C:/Documents and Settings/Michele/Desktop/demofile.xls");
		test.read();

	}

} 
